package com.bree.erp.jsf.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.bree.erp.entity.PoAdjustment;
import com.bree.erp.entity.PurchaseOrder;
import com.bree.erp.service.PoAdjustmentService;
import com.bree.erp.service.PurchaseOrderService;

@ManagedBean(name = "dtPurchaseOrderAdjustmetView")
@ViewScoped
public class PurchaseOrderAdjustmentView {
	
	@EJB
	private PoAdjustmentService adjustmentService;
	@EJB
	private PurchaseOrderService purchaseOrderService;
	
	private PurchaseOrder purchaseOrder;
	private List<PoAdjustment> adjustments;
	private List<PoAdjustment> fordeletion;
	private boolean dirty;
	
	private String description;
	private double amount;

	@PostConstruct
	public void init() {
		fordeletion = new ArrayList<PoAdjustment>();
	}
	
	public double total() {
		double total = 0.0;
		
		if(adjustments != null) {
			for(PoAdjustment item : adjustments) {
				total += item.getAmount();
			}
		}
		
		return total;
	}

	private void recount() {
		
		int count = 0;
		for(PoAdjustment item : adjustments) {
			item.setItemNumber(++count);
		}
		
	}

	public void load(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
		adjustments = new ArrayList<PoAdjustment>(
				purchaseOrder.getAdjustments());
		
		recount();
	}

	public void add() {
		PoAdjustment adjustment = new PoAdjustment();

		adjustment.setId(new Date().getTime() * -1);
		adjustment.setDescription(description);
		adjustment.setAmount(amount);
		adjustment.setItemNumber(adjustments.size() + 1);

		adjustments.add(adjustment);
		
		this.setDirty(true);
	}

	public void delete(Long id) {

		PoAdjustment template = new PoAdjustment();
		template.setId(id);

		if (id > 0) {
			fordeletion.add(template);
		}
		
		adjustments.remove(template);
		
		this.setDirty(true);
		
		recount();
	}
	
	public void save() {
		
		// do deletion 
		for(PoAdjustment delete : fordeletion) {
			adjustmentService.delete(delete.getId());
		}
		
		Long poId = purchaseOrder.getId();
		
		// add new
		for(PoAdjustment adjustment : adjustments) {
			if(adjustment.getId() < 0) {
				adjustmentService.create(poId, adjustment.getDescription(), adjustment.getAmount());
			}
		}
		
		// reload/invalidate cache
		purchaseOrderService.read(poId, true);
	}

	/**
	 * @return the adjustments
	 */
	public List<PoAdjustment> getAdjustments() {
		return adjustments;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the amount
	 */
	public double getAmount() {
		return amount;
	}

	/**
	 * @param amount the amount to set
	 */
	public void setAmount(double amount) {
		this.amount = amount;
	}

	/**
	 * @return the dirty
	 */
	public boolean isDirty() {
		return dirty;
	}

	/**
	 * @param dirty the dirty to set
	 */
	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}

	/**
	 * @return the fordeletion
	 */
	public List<PoAdjustment> getFordeletion() {
		return fordeletion;
	}

}
